package com.xbjtech.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xbjtech.model.Region;
import com.xbjtech.service.RegionService;

@Controller
public class RegionController {
	
	@Autowired
	RegionService regionService;
	
	@GetMapping("/region")
	public ModelAndView region() {
		Long regionId = 130000L;
		List<Region> provinces = regionService.selectRegionsByParId(0L);
		ModelAndView mv = new ModelAndView("region");
		mv.addObject("provinces", provinces);
		Long provinceId = regionId / 10000 * 10000;
		mv.addObject("provinceId", provinceId);
		List<Region> cityList = regionService.selectRegionsByParId(provinceId);
		mv.addObject("cityList", cityList);
		if (regionId % 10000 != 0) {
			Long cityId = regionId / 100 * 100;
			mv.addObject("cityId", cityId);
			List<Region> countryList = regionService.selectRegionsByParId(cityId);
			mv.addObject("countryList", countryList);
			if (regionId % 100 != 0) {
				mv.addObject("countryId", regionId);
			}
		}
		return mv;
	}
	
	@RequestMapping("/getChildRegion")
	@ResponseBody
	public List<Region> getRegionsByParid(@RequestParam Long parid) {
		return regionService.selectRegionsByParId(parid);
	}
	
}
